<?php
namespace Activity\Models;

class RedPacketLog extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    public function getSource()
    {
        return 'daishu_red_packet_log';
    }

    /**
     * 获取用户领取记录
     *
     * @param $uid 用户id
     *
     * @return array|bool
     */
    public function getSumRedPacketByUid($uid)
    {
        try {
            $query = 'SELECT SUM(`money`) AS  `money` FROM `daishu_red_packet_log` WHERE `uid` = ' . $uid;
            $ret   = $this->db()->query($query)->fetch();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return $ret;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 查看今天是否领取
     *
     * @param $uid 用户id
     *
     * @return bool
     */
    public function isGetByToday($uid)
    {
        try {
            $query = 'SELECT `money` FROM `daishu_red_packet_log` WHERE `uid` = ' . $uid . ' AND TO_DAYS(`create_at`) = TO_DAYS(NOW())';
            $ret   = $this->db()->query($query)->fetchAll();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return true;
            }
        } catch (\PDOException $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 添加红包日志并增加到余额
     *
     * @param $entity
     *
     * @return bool
     */
    public function add($entity)
    {
        try {
            $this->db()->begin();
            if ($this->db()->insertAsDict(self::getSource(), $entity) === false) {
                $this->db()->rollback();
                return false;
            }
            $query = 'SELECT * FROM `daishu_red_packet_manage` WHERE `uid`=' . $entity['uid'];
            $ret   = $this->db()->query($query)->fetch();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                $update = 'UPDATE `daishu_red_packet_manage` SET `money`=`money`+' . $entity['money'] . ' WHERE `uid`=' . $entity['uid'];
                if ($this->db()->execute($update) === false) {
                    $this->db()->rollback();
                    return false;
                }
            } else {
                if ($this->db()->insertAsDict('daishu_red_packet_manage', $entity) == false) {
                    $this->db()->rollback();
                    return false;
                }
            }
            $this->db()->commit();
            return true;
        } catch (\PDOException $ex) {
            $this->errorLog('INSERT OR UPDATE', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
    }

    /**
     * 获取红包领取记录
     *
     * @param $uid
     * @param $limit
     * @param $offset
     *
     * @return bool
     */
    public function getLog($uid, $limit, $offset)
    {
        try {
            $query = $this->select('*')->from(self::class)->where('uid=:uid:', ['uid' => $uid])->limit($limit, $offset);
            $ret   = $query->getQuery()->execute()->toArray();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return $ret;
            }
        } catch (\Exception $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 获取红包总额度
     *
     * @param $uid
     *
     * @return bool
     */
    public function sumMoney($uid)
    {
        try {
            $query = $this->select('SUM(money) as money')->from(self::class)->where('uid=:uid:', ['uid' => $uid]);
            $ret   = $query->getQuery()->execute()->toArray();
            if ($ret !== false && is_array($ret) && count($ret) > 0) {
                return $ret[0];
            }
        } catch (\Exception $ex) {
            $this->errorLog('SELECT', $this->db()->getSQLStatement(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }
}